package com.wocai.jrt.orgadmin.employee.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vteba.web.action.GenericAction;
import com.wocai.jrt.orgadmin.employee.model.Track;
import com.wocai.jrt.orgadmin.employee.service.spi.TrackService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

/**
 * 员工轨迹控制器
 * @author yinlei
 * @date 2015-8-4 15:49:29
 */
@Controller
@RequestMapping("/track")
public class TrackAction extends GenericAction<Track> {
	private static final Logger LOGGER = LoggerFactory.getLogger(TrackAction.class);
	
	@Inject
	private TrackService trackServiceImpl;
	
	/**
     * 获得员工轨迹List，初始化列表页。
     * @param model 参数
     * @return 员工轨迹List
     */
    @RequestMapping("/list")
    public String list(Track model, Map<String, Object> maps) {
    	try {
    		List<Track> list = trackServiceImpl.pagedList(model);
            maps.put("list", list);
		} catch (Exception e) {
			LOGGER.error("get record list error, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
        return "track/list";
    }
    
    @ResponseBody
    @RequestMapping("/query")
    public Map<String, Object> query(@RequestBody Track track) {
    	Map<String, Object> result = new HashMap<String, Object>();
    	try {
    		track.setOrgId(UserUtils.getOrgId());
    		List<Track> list = trackServiceImpl.pagedList(track);
    		
    		// 统计的时候，不需要分页
    		track.setStart(null);
    		long totalRecord = trackServiceImpl.countBy(track);
    		
    		if (LOGGER.isDebugEnabled()) {
    			LOGGER.debug("user query success.");
    		}
    		
    		// jQuery datatable 就是这样的参数
    		result.put("aaData", list); // 返回的数据
    		result.put("iTotalRecords", totalRecord); // 总记录数
    		result.put("iTotalDisplayRecords", totalRecord); // 显示的记录数
		} catch (Exception e) {
			LOGGER.error("track query error, msg=[{}]", e.getMessage());
		}
        return result;
    }
    
    /**
     * 查看员工轨迹详情页。
     * @param model 查询参数，携带ID
     * @return 员工轨迹详情页
     */
    @RequestMapping("/detail")
    public String detail(Track model, Map<String, Object> maps) {
    	try {
    		model = trackServiceImpl.get(model.getId());
            maps.put("model", model);//将model放入视图中，供页面视图使用
		} catch (Exception e) {
			LOGGER.error("query record detail, errorMsg=[{}].", e.getMessage());
			return "common/error";
		}
        return "track/detail";
    }
}
